Repository navigation
Fix #15219 FP constVariablePointer (pointer assignment with offset) - #9033
chrchr-github wants to merge 5 commits into
Conversation
|
Test results for commit 77ceb53 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9033/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The FP from the ticket is fixed, and so are the related FPs for writes through p - 1 (*(p - 1) = 0;, int* q = p - 1; *q = 0;). But the PR adds new false negatives. A pointer subtraction that is only read is no longer reported: return *(p - 1);, char c = *(p - 1); and return (p - 1)[0]; all warned before and are silent now, while *(p + 1) still warns. The deref detection at the top of the loop handles + (hasIncDecPlus) but not -, so a deref of p - n now goes to the non-deref branch and marks p as non-const. The new valueType condition also drops warnings when the - has no valueType, for example p - n > 0 where n has an unknown type. Most of the 23 warnings that the PR test runner lists as removed are probably these cases.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestOther passes: 0 failures, 255 tests.
I compared the two builds with cppcheck -q --enable=style --template='{line}: {id} {message}' FILE.
a.cpp
int f1(int* p) { return *(p - 1); }
int f2(int* p) { return *(p + 1); }
void f3(int* p) { *(p - 1) = 0; }
int f4(int* p) { const int* q = p - 1; return *q; }
int f5(int* p) { const int* q = p + 1; return *q; }
long f6(int* p, int* q) { return p - q; }
int f7(int* p, int n) { return p[-n]; }
int g(const int*);
int f8(int* p) { return g(p - 1); }
int f9(int* p) { return g(p + 1); }
int* f10(int* s) { int* q = s - 1; return q; }
int f11(int* p) { int* q = p - 1; return *q; }
void f12(int* p) { int* q = p - 1; *q = 0; }- base warns on
pin lines 1, 2, 4, 6, 7, 9, 10, 11, 12, 13, onqin line 6, onsin line 11, and givesconstVariablePointerforqin line 12. The warnings on lines 3, 11 and 13 are false positives. - pr warns only on lines 2, 6 (
pandq), 7, 9, 10, and givesconstVariablePointerforqin line 12. - So the PR fixes the FPs on lines 3, 11 and 13. Line 1 (
return *(p - 1);) is a new false negative, while line 2 (*(p + 1)) is still reported.
b.cpp
int f1(int* p, unknown_t n) { return *(p - n); }
int f2(int* p, unknown_t n) { int x = p - n > 0; return x; }
int f3(char* p) { char c = *(p - 1); return c; }
int f4(int* p) { return (p - 1)[0]; }- base gives
constParameterPointerforpon all 4 lines. - pr gives no warning at all. These are new false negatives.
Automated review of 77ceb53 by Claude. The pull request was built and tested locally.
| if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;")) | ||
| if (Token::Match(parent, "%oror%|%comp%|&&|?|!|<<|;")) | ||
| continue; | ||
| if (Token::simpleMatch(parent, "-") && parent->valueType() && parent->valueType()->pointer == 0) |
There was a problem hiding this comment.
AI review
This drops warnings when the pointer subtraction is only read. int f(int* p) { return *(p - 1); }, char c = *(p - 1); and return (p - 1)[0]; all gave constParameterPointer before this PR and give nothing now, while *(p + 1) is still reported. The deref detection above (around line 2021) handles + through hasIncDecPlus but not -, so *(p - 1) reaches this branch and marks p as non-const.
Treating - like + there (parent->str() == "+" || parent->str() == "-") should keep these warnings and still fix #15219.
Also, when the - token has no valueType (e.g. p - n > 0 with n of an unknown type), this no longer continues, so that case is a new false negative too.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
I found no new problems. The FP from #15219 is fixed: int* q = s - 1; return q; and writes through p - 1 are no longer reported. The main point from the earlier review is also fixed, because *(p - 1) that is only read warns again. Two false negatives from that review are still there. (p - 1)[0] that is only read gets no warning, and neither does p - n > 0 where - has no valueType. The [ case is not specific to -: (p + 1)[0] is not reported on main either. The same holds for const int* q = p - 1;, return p - 1; from a function that returns const int*, and p - 1 == q. The PR now handles - the same way as +, which keeps these existing false negatives.
Earlier comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Read-only deref of pointer subtraction (*(p - 1)) no longer reported | AI review | general | ✅ Fixed |
| 2 | (p - 1)[0] read-only no longer reported | AI review | general | |
| 3 | p - n with no valueType (unknown n) no longer reported | AI review | general | |
| 4 | Deref detection ignores '-', so *(p - 1) reads mark p non-const | AI review | lib/checkother.cpp:2094 |
✅ Fixed |
| 5 | '-' without valueType no longer continues (p - n > 0 with unknown n) | AI review | lib/checkother.cpp:2094 |
@chrchr-github, please fix, resolve or reply to the open comments: #2, #3, #5.
For #2, #3, #5 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. ./testrunner TestOther passes with 0 failures.
I compared the two builds with cppcheck -q --enable=style --template='{line}: {id} {message}' on scratch files with about 30 functions that use p - n in different ways.
False positives on main that the PR fixes:
int* q = s - 1; return q;int* q = p - 1; *q = 0;(p - 1)[0] = 1;(*(p - 1))++;int* f(int* p) { return p - 1; }
Cases where both builds give the same result:
- These still warn:
return *(p - 1);,char c = *(p - 1);,g(p - 1)wheregtakesconst int*,*(p - n)with an unknownn, ande - p.
New true positive in the PR:
return (p - 1)->x;
Warnings that main gives and the PR drops:
| Code | Same code with + on main |
|---|---|
return (p - 1)[0]; |
(p + 1)[0] is not reported |
int x = p - n > 0; with unknown n |
— |
const int* q = p - 1; |
not reported |
const int* f(int* p) { return p - 1; } |
not reported |
return p - 1 == q; |
not reported |
Automated review of 5db5d7f by Claude. The pull request was built and tested locally.
|
Related tickets: https://trac.cppcheck.net/ticket/15226 (FP), https://trac.cppcheck.net/ticket/15227 (FN) |
|
Test results for commit 5db5d7f (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9033/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
No description provided.